package com.where_to_play.adapter;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.where_to_play.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.where_to_play_search.Search_Webview;

public class Search_listview_Adapter extends BaseAdapter {
	private List<Map<String, String>> list;
	private Context context;
	private LayoutInflater inflater;
	private BitmapUtils bitmapUtils;

	public Search_listview_Adapter(List<Map<String, String>> list,
			Context context) {
		super();
		this.list = list;
		this.context = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		String diskCachePath = context.getExternalCacheDir().getAbsolutePath();
		bitmapUtils = new BitmapUtils(context, diskCachePath)
				.configMemoryCacheEnabled(true)
				.configDefaultCacheExpiry(1024 * 1024 * 4)
				.configDefaultBitmapMaxSize(300, 300)
				.configDefaultBitmapConfig(Config.ALPHA_8)
				.configDefaultLoadingImage(R.drawable.ic_stub)
				.configDefaultLoadFailedImage(R.drawable.ic_error)
				.configThreadPoolSize(5)
				.configDefaultImageLoadAnimation(
						AnimationUtils.loadAnimation(context, R.anim.fade_in));

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	class Viewholder_Search {
		@ViewInject(R.id.search_listview_item_relativelayout)
		RelativeLayout search_listview_item_relativelayout;
		@ViewInject(R.id.imageview_search_listview_item)
		ImageView imageview_search_listview_item;
		@ViewInject(R.id.textview_Top_search_listview_item)
		TextView textview_Top_search_listview_item;
		@ViewInject(R.id.textview_Bottom_search_listview_item)
		TextView textview_Bottom_search_listview_item;
		@ViewInject(R.id.radiobutton_search_listview_item)
		RadioButton radiobutton_search_listview_item;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Viewholder_Search viewholder_Search;
		if (convertView == null) {
			viewholder_Search = new Viewholder_Search();
			convertView = inflater.inflate(R.layout.search_listview_item,
					parent, false);
			ViewUtils.inject(viewholder_Search, convertView);
			convertView.setTag(viewholder_Search);
		} else {
			viewholder_Search = (Viewholder_Search) convertView.getTag();
		}
		String imgurl = list.get(position).get("imgUrl");
		bitmapUtils.display(viewholder_Search.imageview_search_listview_item,
				imgurl);
		viewholder_Search.radiobutton_search_listview_item.setText(list.get(
				position).get("favoriteNum"));
		viewholder_Search.textview_Top_search_listview_item.setText(list.get(
				position).get("title"));

		viewholder_Search.textview_Bottom_search_listview_item.setText(list
				.get(position).get("labels"));
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, Search_Webview.class);
				Bundle bundle = new Bundle();
				bundle.putString("shareUrl", list.get(position).get("shareUrl"));
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	public void reloadListView(Collection<? extends Map<String, String>> data,
			boolean isClear) {
		if (isClear) {
			list.clear();
		}
		list.addAll(data);
		notifyDataSetChanged();

	}
}
